package xin.fallen.util;

import org.ini4j.Config;
import org.ini4j.Ini;
import org.ini4j.Profile.Section;

import java.io.File;

/**
 * Author: Fallen
 * Date: 2017/3/10
 * Time: 11:59
 * Usage:
 */
public class IniUtil {
    private static Ini ini;

    public static void init() {
        Config cfg = new Config();
        cfg.setMultiSection(false);// 设置Section允许出现重复
        ini = new Ini();
        ini.setConfig(cfg);
        try {
            File file = FileFinder.find("codeconf.ini");
            ini.load(file);
        } catch (Exception e) {
            throw new RuntimeException("ini文件初始化失败");
        }
    }

    public static String getValue(String section, String key) {
        System.out.println("IniUtil：Section->" + section + "  key->" + key);
        if (ini == null) {
            throw new RuntimeException("请先执行init方法初始化参数");
        }
        Section s = ini.get(section);
        String res = s.get(key);
        return res == null ? "" : res;
    }
}
